import sys,os
from time import sleep


def alterFileLine(file, target_old_str, new_line):
    # replace the line containing 'old_str' with 'new_str'

    with open(file, "r") as f1, open(f"{file}.bak", "w", encoding="utf-8") as f2:
        for line in f1:
            if target_old_str in line:
                f2.write(new_line)
            else:
                f2.write(line)
    os.remove(file)
    os.rename("%s.bak" % file, file)

# 获取学生学号
studentId=''
if len(sys.argv)>1:
    studentId=sys.argv[1]
    print(f"welcome student {studentId}\nstarting server")
else:
    print('no studentId (command should be like "python main.py YOUR_STUDENT_ID")')


# 修改Django配置文件，使得服务器使用本学生文件夹数据库
alterFileLine(
    r'./Django/SUIBEER/SUIBEER/settings.py',
    "'NAME': BASE_DIR",
    f"        'NAME': BASE_DIR / r'../../../SUIBEER_Students/student_{studentId}/courses/courses.sqlite3',\n"
)

